/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * For testing purpose
 * @author: jin Chen 
 * @date: Oct 13, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.selectcontrol;

import edu.psu.geovista.app.pcp.toolkits.selectcontrol.ui.VerticalBttnsPane;
import edu.psu.geovista.app.pcp.toolkits.table.ConfigureTableModel;

import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.Vector;

public class SimpleSelectItemsPane extends SelectItemsPane {



    public SimpleSelectItemsPane() {


        this.initLeftTable() ;
        this.initRightTable() ;
    }
    public  String[] getSelectedItemNames() throws Exception{
          Object[] os= rightTable.getSortValueAtColumn("Name");
        String[] itemNames=new String[os.length ];
        for (int i=0;i<itemNames.length ;i++){
            itemNames[i]=(String)os[i];
        }

        return itemNames;
     }
    // 
    protected void initButtons() {
        /*String[] names=new String[]{"Show","Hide","Show all","Hide all","Up","Down","Top","Bottom","Apply"};
        this.addButtons(names);*/


        VerticalBttnsPane selectP = createSelectBttnsPane();
        VerticalBttnsPane updnP = createUpDownBttnsPane();

        btnP.setLayout(new BorderLayout());
        btnP.add(selectP);

        /*JPanel p=new JPanel();
        p.setLayout(new BorderLayout());
        p.add(updnP) ;*/
        this.RTableP.add(updnP, BorderLayout.EAST );



    }

    

    protected void initLeftTable() {
        TableModel model=getSampleModel();
        this.leftTable.setModel(model);



    }

    //1.create tableModel //2. init values
    protected void initRightTable() {
        //1. Model
        int initRowCount=0;
        Vector colName=new Vector();
        colName.add("Name");colName.add("NumRec");
        Class[] types = new Class [] {
                 String.class, Integer.class
        };
        boolean[] editable={false,false,false};
        ConfigureTableModel model=new ConfigureTableModel(colName,initRowCount);
        model.setTypes(types);
        model.setEditables(editable);
        rightTable.setModel(model);

        leftTable.setColumnsWidth(new int[]{1,2});
        rightTable.setColumnsWidth(new int[]{2,1});


    }

    public void setSize() {
         int w=rightTable.getSize().width ;
        TableColumn ncol = rightTable.getColumnModel().getColumn(0); //.getColumn("Name");
        ncol.setPreferredWidth(w*2/3);
        rightTable.repaint();
    }

    protected void actOnButtonClick(String btnName) {
        //"Right","Left","Right all","Left all","Up","Down","Apply"
        if(btnName.equals("Show") ){
            this.moveRight();
        }
        else if(btnName.equals("Hide") ){
            this.moveLeft() ;
        }
        else if(btnName.equals("Up")){
            this.moveUp();

        }
        else if(btnName.equals("Down")){
            this.moveDown() ;
        }
        else if (btnName.equals("Show all")){
            this.moveAllRight() ;
        }
        else if (btnName.equals("Hide all")){
             this.moveAllLeft() ;
        }
        else if (btnName.equals("Top")){
             this.moveTop() ;
             System.out.println("Top");
        }
         else if (btnName.equals("Bottom")){
             this.moveBottom() ;
             System.out.println("Bottom");
        }
         else if (btnName.equals("Apply")){
             this.apply() ;
             System.out.println("Apply");
        }
    }
    /*******************************************************************************************************
     *                button actions
     *******************************************************************************************************/
    /**
     * Add a record to the right table. The record contain the ID value of the record in left table and an constant value 1000
     * work together with intiTable()
     *
     * @param data
     */
    public void selectRecord(Object[] data) {
        String id=(String) data[rightTable.getIDColumnIndex() ];
        Object[] arow={id, new Integer(1000)};   //assume ID column is the 1st column
                this.rightTable.addRow(arow);
        //Object[] arow={columnName, new Integer(rowCount) };
    }
    public  static TableModel getSampleModel(){
        Vector colName=new Vector();
        colName.add("Name");colName.add("Description");
        Class[] types = new Class [] {
                 String.class, String.class
        };

        String [] rec1={"row1","des1"};
        String [] rec2={"row2","des2"};
        String [] rec3={"row3","des3"};
        Object[][] data=new Object[][]{rec1,rec2,rec3};

        String [] colnames=new String[]{"Name","Description"};


        boolean[] editable={false,false,false};
        ConfigureTableModel model=new ConfigureTableModel(data,colnames);
        model.setTypes(types);
        model.setEditables(editable);
        return model;
    }


}
